package com.app.service.impl;

import com.app.mapper.PositionMapper;
import com.app.mapper.ShareLikesComMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Description:行业
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class ShareLikesComServiceImpl implements ShareLikesComService {

    @Autowired

    ShareLikesComMapper shareLikesComMapper;
    @Autowired
    ShareLikesComService shareLikesComService;
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;
    @Autowired
    ComRecruitersService comRecruitersService;

    @Override
    public int insert(ShareLikesComModel model) {
        return shareLikesComMapper.insert(model);
    }

    @Override
    public Object insertLikes(int type, int circleId, String comments, HttpServletRequest request) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405, "token为空");

        }
        TokenUtils tokenUtil = new TokenUtils();
        String mobile = tokenUtil.verify(token);
        if (mobile.equals("false")){
            return Result.error(500, "系统错误");
        }
        UserModel user = userService.getUserByMobile(mobile);
        int identity = Integer.valueOf(request.getParameter("identity"));
        ShareLikesComModel shareLikesCom = new ShareLikesComModel();

        if (identity == 1){
         CustomerModel customerModel= customerService.getCustomerById(user.getCId());
            shareLikesCom.setRealName(customerModel.getRealName());
        }else {
          ComRecruitersModel comRecruitersModel=  comRecruitersService.getComRecruitersById(user.getRId());
            shareLikesCom.setRealName(comRecruitersModel.getRealName());
        }
        shareLikesCom.setType(type);
        shareLikesCom.setCircleId(circleId);
        shareLikesCom.setUserId(user.getId());
        shareLikesCom.setComments(comments);
        if(type==1)
        {
            ShareLikesComModel model=  shareLikesComService.findIsLikes(circleId,user.getId());
            if(model==null){
                shareLikesCom.setCrtTime(CommonHelp.getNowTimestamp());
                shareLikesComService.insert(shareLikesCom);
                return null;
            }
            else {
                return Result.error(500,"不可以重复点赞");
            }
        }else {
            shareLikesCom.setCrtTime(CommonHelp.getNowTimestamp());
            shareLikesComService.insert(shareLikesCom);
            return null;
        }

    }

    @Override
    public Object delLikeAndCom(int circleId, int type, HttpServletRequest request) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405, "token不存在");

        }
        TokenUtils tokenUtil = new TokenUtils();
        String mobile = tokenUtil.verify(token);
        if (mobile.equals("false")){
            return null;
        }
        UserModel user = userService.getUserByMobile(mobile);

        ShareLikesComModel shareLikesCom = new ShareLikesComModel();


        shareLikesCom.setCircleId(circleId);
        shareLikesCom.setUserId(user.getId());
        shareLikesCom.setType(type);
        shareLikesComMapper.deleteLikeByUid(type,circleId,user.getId());
        return null;
    }

    @Override
    public int likeCount(int circleId) {
        QueryWrapper<ShareLikesComModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("circle_id", circleId).eq("type",1);
        int count = shareLikesComMapper.selectCount(queryWrapper);
        if(count>0){
            return count;
        }
        return 0;
    }

    @Override
    public ShareLikesComModel findIsLikes(int circleId, int userId) {
        QueryWrapper<ShareLikesComModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("circle_id", circleId).eq("type",1).eq("user_id",userId);
        ShareLikesComModel model = shareLikesComMapper.selectOne(queryWrapper);
        if(model==null){
            return null;
        }
        return model;
    }

    @Override
    public List<ShareLikesComModel> selectComById(int id) {
        QueryWrapper<ShareLikesComModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("circle_id", id).eq("type", 2).orderByAsc("crt_time");
        List<ShareLikesComModel> comList = shareLikesComMapper.selectList(queryWrapper);
        if (comList != null) {
            for (int j = 0; j < comList.size(); j++) {
                if (comList.get(j).getRealName().length() == 2) {
                    String starName = comList.get(j).getRealName().substring(0, 1) + "*";
                    comList.get(j).setStarName(starName);
                } else {
                    String starName = comList.get(j).getRealName().substring(0, 1) + "**";
                    comList.get(j).setStarName(starName);
                }
            }
        }

        return comList;
    }
    @Override
    public Object delCom(int id) {
        ShareLikesComModel shareLikesCom = shareLikesComMapper.selectById(id);
        if(shareLikesCom == null){
            return Result.error(500, "该条数据不存在");
        }else {
            shareLikesComMapper.deleteById(id);
        }
        return null;
    }

}
